package com.fastloan.mgt.product.label.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.product.label.dao.ProductLabelMapper;
import com.fastloan.mgt.product.label.service.ProductLabelService;
import com.fastloan.mgt.product.label.model.ProductLabel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * @author admins
 *         createTime 2017/12/27
 */
@Service
public class ProductLabelServiceImpl extends BasicServiceImpl<ProductLabel> implements ProductLabelService {
    @Autowired
    private ProductLabelMapper productLabelMapper;

    @Override
    @Transactional
    public void insertProductLabel(String productId, String labels) {
        Arrays.stream(labels.split(","))
                .forEach(labelId -> {
                    ProductLabel productLabel = new ProductLabel();
                    productLabel.setProductId(productId);
                    productLabel.setLabelId(labelId);
                    insert(productLabel);
                });
    }

    @Override
    @Transactional
    public void updateProductLabel(String productId, String labels) {
        ProductLabel productLabel = new ProductLabel();
        productLabel.setProductId(productId);
        delete(productLabel);
        insertProductLabel(productId, labels);
    }

}
